package com.thx.information.action;

import com.thx.common.struts2.CurdAction;
import com.thx.common.web.ActionResult;
import com.thx.information.model.InfoCommentLog;
import com.thx.information.service.CommentLogService;
import com.thx.log.LogUtil;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.Date;

@ParentPackage(value = "gloab-package")
@Namespace(value = "/information")
@Action(value = "comment",
    results = {@Result(name = "list", location = "/pages/information/comment/list.jsp"),
        @Result(name = "input", location = "/pages/information/comment/input.jsp"),
        @Result(name = "json", type = "json", params = {"root", "result"})},
    interceptorRefs = {@InterceptorRef(value = "topDtStack"),
        @InterceptorRef(value = "pageInterceptor", params = {"includeMethods", "list"}),})
@Controller
@Scope("prototype")
public class CommentLogAction extends CurdAction<InfoCommentLog> {

  private static final long serialVersionUID = 1L;
  private static final Logger logger = LoggerFactory.getLogger(CommentLogAction.class);

  @Autowired
  private CommentLogService service;

  private String ids;
  private String treeNodes;
  private String pid;
  private String searchName;

  // 测试数据
  private Long enterpriseId = 181L;
  private Long personnelId = 1048L;
  private String personnelName = "管理员";
  private String deptName = "部门";
  private Long depId = 1L;
  private Long role = 1L;
  private Boolean ifAdmin = true;


  protected void doList() {
    logger.debug("<进入>:doList方法  显示评论信息");
    logger.debug("<入参>:pid={}", pid);
    try {
      Long infoId = NumberUtils.toLong(pid);
      list = service.findByInfoId(infoId);
      logger.debug("<出参>查询结果{}：", list);
    } catch (Exception e) {
      logger.error("<错误>:查询异常");
    }
  }

  /**
   * 新增、修改显示页面.
   * 
   */
  public String input() {
    logger.debug("<进入>:评论修改/新增跳转方法");
    logger.info("<入参>:pid={},id={}", pid, id);
    try {
      if (StringUtils.isNotBlank(id)) {
        entity = service.get(Long.parseLong(id));
      }
      logger.debug("<出参>查询结果{}：", entity);
    } catch (Exception e) {
      logger.error("<错误>:查询异常");
    }
    return "input";
  }

  /**
   * 新增修改方法.
   */
  public void doSave() {
    try {
      if (StringUtils.isBlank(id)) {
        logger.debug("<进入>:评论管理新增方法");
        logger.info("<入参>:新增数据={},pid={}", entity, pid);
        Long infoId = NumberUtils.toLong(pid);

        // 评论基本信息
        InfoCommentLog entityd = new InfoCommentLog();
        BeanUtils.copyProperties(entity, entityd);

        entityd.setEnterpriseId(enterpriseId);
        entityd.setPersonnelId(personnelId);
        entityd.setInfoId(infoId);
        entityd.setDeptId(depId);
        entityd.setPerName(personnelName);
        entityd.setDeptName(deptName);
        entityd.setCommentDate(new Date());

        service.save(entityd);
        setResult(SUCCESS, "评论成功！");
        LogUtil.log("评论信息管理", LogUtil.ACTION_UPDATE, LogUtil.RESULT_SUCCESS, "新增信息");
      } else {
        logger.debug("<进入>:评论管理修改方法");
        logger.info("<入参>:修改信息id={}", entity);
        InfoCommentLog entityd = service.get(Long.parseLong(id));
        BeanUtils.copyProperties(entity, entityd, new String[] {"commentLogId", "personnelId",
            "infoId", "enterpriseId", "deptId", "commentDate", "perName", "deptName"});

        service.update(entityd);
        setResult(SUCCESS, "修改成功！");
        LogUtil.log("评论信息管理", LogUtil.ACTION_UPDATE, LogUtil.RESULT_SUCCESS, "修改信息");
      }
    } catch (Exception e) {
      if (StringUtils.isNotBlank(id)) {
        LogUtil.log("评论信息管理", LogUtil.ACTION_UPDATE, LogUtil.RESULT_FAIL, "修改信息");
        logger.error("<错误>:修改异常{}", e);
      } else {
        LogUtil.log("评论信息管理", LogUtil.ACTION_UPDATE, LogUtil.RESULT_FAIL, "新增信息");
        logger.error("<错误>:新增异常{}", e);
      }
    }
  }


  protected void doDelete() {
    logger.debug("<进入>:删除信息方法");
    try {
      if (StringUtils.isNotBlank(id)) {
        logger.debug("<入参>:信息数据id={}", id);
        service.delete(NumberUtils.toLong(id));
        setResult(ActionResult.SUCCESS, "删除成功！");
        LogUtil.log("评论信息管理", LogUtil.ACTION_DELETE, LogUtil.RESULT_SUCCESS, "删除评论信息");
      } else {
        setResult(ActionResult.FAIL, "服务器异常！");
      }
    } catch (Exception e) {
      logger.error("<错误>:删除异常{}", e);
      setResult(ActionResult.FAIL, "服务器异常！");
    }
  }

  /**
   * 批量删除信息.
   */
  protected void doDelMulti() {
    logger.debug("<进入>:批量删除信息方法");
    try {
      if (ids != null && ids.split(",").length > 0) {
        logger.debug("<入参>:批量删除信息id={}", ids);
        for (String del : ids.split(",")) {
          service.delete(NumberUtils.toLong(del));
        }
        setResult(ActionResult.SUCCESS, "批量删除成功！");
        LogUtil.log("评论信息管理", LogUtil.ACTION_DELETE, LogUtil.RESULT_SUCCESS, "批量删除信息");
      } else {
        setResult(ActionResult.FAIL, "服务器异常！");
      }
    } catch (Exception e) {
      logger.error("<错误>:批量删除异常{}", e);
      setResult(ActionResult.FAIL, "服务器异常！");
    }
  }


  // /**
  // * 新建评论 建立日期 : Apr 23, 2013 5:41:50 PM<br>
  // * 作者 : wangamin<br>
  // *
  // * @return
  // */
  // public String saveUpdateInfo() {
  // logger.debug("<进入>:saveUpdateInfo方法 新建评论");
  // try {
  // Long enterpriseId = (Long) getSession().getAttribute("enterpriseId");
  // Personnel p = (Personnel) getSession().getAttribute("personnel");
  // infoCommentLog.setPerName(p.getName());
  // infoCommentLog.setCommentDate(new Date());
  // infoCommentLog.setEnterpriseId(enterpriseId);
  // infoCommentLog.setPersonnelId(p.getPersonnelId());
  // infoCommentLog.setDeptId(p.getDeptId());
  // infoCommentLog.setDeptName(p.getDeptName());
  // service.save(infoCommentLog);
  // inifid = infoCommentLog.getInfoId();
  // getSession().setAttribute("commentSucces", "1");
  // } catch (Exception e) {
  // logger.error("<错误>:新建评论错误", e);
  // }
  // return "save";
  // }


  public String getIds() {
    return ids;
  }

  public void setIds(String ids) {
    this.ids = ids;
  }

  public String getTreeNodes() {
    return treeNodes;
  }

  public void setTreeNodes(String treeNodes) {
    this.treeNodes = treeNodes;
  }

  public String getPid() {
    return pid;
  }

  public void setPid(String pid) {
    this.pid = pid;
  }

  public String getSearchName() {
    return searchName;
  }

  public void setSearchName(String searchName) {
    this.searchName = searchName;
  }

}
